#define BLINKER_WIFI
#define BLINKER_MIOT_LIGHT
#define BLINKER_MIOT_SENSOR   //定义为语音控制传感器设备 
#include <Blinker.h>
#include <Servo.h>
#include <DHT.h>

#define dj 5
Servo myservo;//定义舵机

#define DHTPIN 2 //
#define DHTTYPE DHT11   // DHT 11


char auth[] = "a3c11b64eb38";//根据需要自行修改
bool WIFI_Status = true;

DHT dht(DHTPIN, DHTTYPE);
 
int32_t humi_read = 0;//湿度 小爱同学只能查整数的数据 
float temp_read = 0; //温度

// 新建组件对象
BlinkerButton Button1("test");
BlinkerNumber HUMI("humi");
BlinkerNumber TEMP("temp");

int counter = 0;

//配网函数
void smartConfig()
{
  WiFi.mode(WIFI_STA);
  Serial.println("\r\nWait for Smartconfig...");
  WiFi.beginSmartConfig();//等待手机端发出的用户名与密码
  while (1)
  {
    Serial.print(".");
    digitalWrite(LED_BUILTIN, HIGH);  
    delay(1000);                      
    digitalWrite(LED_BUILTIN, LOW);    
    delay(1000);                      
    if (WiFi.smartConfigDone())//退出等待
    {
      Serial.println("SmartConfig Success");
      Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
      Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
      break;
    }
  }
}

//设置wifi函数
void WIFI_Set()
{
    Serial.println("\r\n正在连接");
    int count = 0;
    while(WiFi.status()!=WL_CONNECTED)
    {
        if(WIFI_Status)
        {
            Serial.print(".");
            digitalWrite(LED_BUILTIN, HIGH);  
            delay(500);                       
            digitalWrite(LED_BUILTIN, LOW);    
            delay(500);                 
            count++;
            if(count>=5)//5s后连接失败
            {
                WIFI_Status = false;
                Serial.println("WiFi连接失败，请用手机进行配网"); 
            }
        }
        else
        {
            smartConfig();  //微信智能配网
        }
     }  
     Serial.println("连接成功");  
     Serial.print("IP:");
     Serial.println(WiFi.localIP());
}

void miotPowerState(const String & state)//电源类操作
{
    BLINKER_LOG("need set power state: ", state);

    if (state == BLINKER_CMD_ON) {

        BlinkerMIOT.powerState("on");
        BlinkerMIOT.print();//反馈状态 
        myservo.write(60);//收到“on”的指令后舵机旋转55度
        delay(1000);
        myservo.write(80);
        Button1.print("on");
 
    }
    else if (state == BLINKER_CMD_OFF) {

         BlinkerMIOT.powerState("off");
         BlinkerMIOT.print(); 
         myservo.write(95);
         delay(1000);
         myservo.write(80); 
         Button1.print("off");  
           
    }
}

// 按下按键即会执行该函数
void button1_callback(const String & state)
{
    BLINKER_LOG("get button state: ", state);
    if (state=="on") 
    {
        myservo.write(60);//收到“on”的指令后舵机旋转55
        delay(1000);
        myservo.write(80);
        Button1.print("on"); 
        
    } else if(state=="off")
    {
       myservo.write(95);//长按开关按键后舵机旋转90
       delay(1000);
       myservo.write(80);
       Button1.print("off");
    }
        
}

void heartbeat()
{
    //反馈湿度数据
    HUMI.print(humi_read);
    //设置ui组件图标和颜色
    HUMI.icon("fas fa-tint");
    HUMI.color("#0099FF ");

    //反馈温度数据
    TEMP.print(temp_read);
    TEMP.icon("fas fa-thermometer-empty");
    TEMP.color("#FF3300");
}
 
 
void miotQuery(int32_t queryCode)
{
    BLINKER_LOG("MIOT Query codes: ", queryCode);

    switch (queryCode)
    {
        //同时查询传感器数据
        case BLINKER_CMD_QUERY_ALL_NUMBER :
            BLINKER_LOG("MIOT Query All");
            BlinkerMIOT.temp(temp_read);
            BlinkerMIOT.humi(humi_read);
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
        //查询pm2.5
        case BLINKER_CMD_QUERY_PM25_NUMBER :
            BLINKER_LOG("MIOT Query PM25");
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.print();
            break;
        //查询湿度
        case BLINKER_CMD_QUERY_HUMI_NUMBER :
            BLINKER_LOG("MIOT Query HUMI");
            BlinkerMIOT.humi(humi_read);
            BlinkerMIOT.print();
            break;
        //查询温度
        case BLINKER_CMD_QUERY_TEMP_NUMBER :
            BLINKER_LOG("MIOT Query TEMP");
            BlinkerMIOT.temp(temp_read);
            BlinkerMIOT.print();
            break;
        //查询CO2
        case BLINKER_CMD_QUERY_TIME_NUMBER :
            BLINKER_LOG("MIOT Query co2");
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
        default :
            BlinkerMIOT.temp(20);
            BlinkerMIOT.humi(20);
            BlinkerMIOT.pm25(20);
            BlinkerMIOT.co2(20);
            BlinkerMIOT.print();
            break;
    }
}



void setup()
{
    // 初始化串口
    Serial.begin(115200);
    BLINKER_DEBUG.stream(Serial);
    
    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, LOW);
    
    // 初始化舵机
    myservo.attach(dj);//舵机的IO口
    myservo.write(80);//上电时舵机75度
    
    BlinkerMIOT.attachPowerState(miotPowerState);
    
    Button1.print("off");
    // 初始化blinker
    Blinker.begin(auth, WiFi.SSID().c_str(), WiFi.psk().c_str());
    Button1.attach(button1_callback);
    Blinker.attachHeartbeat(heartbeat);
    BlinkerMIOT.attachQuery(miotQuery); //小爱同学 数据反馈
    dht.begin();
}

void loop() {
    Blinker.run();
    float h = dht.readHumidity();
    float t = dht.readTemperature();
 
    if (isnan(h) || isnan(t))
    {
        BLINKER_LOG("Failed to read from DHT sensor!");
    }
    else
    {
        BLINKER_LOG("Humidity: ", h, " %");
        BLINKER_LOG("Temperature: ", t, " *C");
        humi_read = h;
        temp_read = t;
    }
 
    Blinker.delay(800);
}
